﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace Web.fanliAdmin.Info
{
    public partial class fanli_helpcataModify : System.Web.UI.Page
    {
        #region DAL
        fanli.DAL.fanli_helpcata dal = new fanli.DAL.fanli_helpcata();
        #endregion

        #region 属性
        /// <summary>
        /// 正在修改记录的ID
        /// </summary>
        public int Id
        {
            get { return ViewState["Id"] == null ? 0 : Convert.ToInt32(ViewState["Id"]); }
            set { ViewState["Id"] = value; }
        }
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            SessionHelp.AssertLogin(this);
            if (!IsPostBack)
            {
                if (Request.QueryString["id"] != null)
                {
                    lblFormTitle.Text = "类别信息修改";
                    int qid = 0;
                    int.TryParse(Request.QueryString["id"], out qid);
                    if (qid != 0)
                    {
                        Id = qid;
                        SetValue();
                    }
                    else
                    {
                        Id = -1;
                        LIB.MessageBox.Show(this, "参数传递有误！");
                    }
                }
                else
                {
                    lblFormTitle.Text = "类别信息添加";
                }
            }
        }

        private void SetValue()
        {
            fanli.Model.fanli_helpcata cataModel = dal.GetModel(Id);
            if (cataModel!=null)
            {
                txtAbout.Text = cataModel.about;
                txtCataname.Text = cataModel.cataname;
                txtSortid.Text = cataModel.sortid.ToString();
            }
        }

        protected void btnSumit_Click(object sender, EventArgs e)
        {
            if (Id == 0)
            {
                Add();
            }
            else
            {
                if (Id==-1)
                {
                    LIB.JsWrite.Alert(this, "id 不存在！");
                    return;
                }
                Modify();
            }
        }

        private void Add()
        {
            #region 获取数据
            string varCataname;	//类别名称
            string varAbout;	//说明
            int varSortid;	//排序
            try
            {
                varCataname = txtCataname.Text.Trim();	//类别名称
                varAbout = txtAbout.Text.Trim();	//说明
                varSortid = int.Parse(txtSortid.Text.Trim());	//排序
            }
            catch
            {
                LIB.JsWrite.Alert(this, "输入数据格式不合法，请检查输入数据。");
                return;
            }

            string where = " cataName=@cataName ";
            List<SqlParameter> paramList = new List<SqlParameter>();
            paramList.Add(new SqlParameter("@cataName", varCataname));
            if (dal.Exists(where,paramList))
            {
                LIB.JsWrite.Alert(this, "名称已经存在。");
                return;
            }
            #endregion

            #region 赋值model
            fanli.Model.fanli_helpcata model = new fanli.Model.fanli_helpcata();
            model.cataname = varCataname;	//类别名称
            model.about = varAbout;	//说明
            model.sortid = varSortid;	//排序
            #endregion

            if (dal.Add(model) > 0)
            {
                LIB.MessageBox.ShowAndClose(this, "添加成功");
            }
            else
            {
                LIB.MessageBox.ShowAndClose(this, "添加失败");
            }
        }

        private void Modify()
        {
            #region 获取数据
            string varCataname;	//类别名称
            string varAbout;	//说明
            int varSortid;	//排序
            try
            {
                varCataname = txtCataname.Text.Trim();	//类别名称
                varAbout = txtAbout.Text.Trim();	//说明
                varSortid = int.Parse(txtSortid.Text.Trim());	//排序
            }
            catch
            {
                LIB.JsWrite.Alert(this, "输入数据格式不合法，请检查输入数据。");
                return;
            }
            #endregion

            #region 赋值model
            string where = " cataName=@cataName and id<>"+Id+" ";
            List<SqlParameter> paramList = new List<SqlParameter>();
            paramList.Add(new SqlParameter("@cataName", varCataname));
            if (dal.Exists(where, paramList))
            {
                LIB.JsWrite.Alert(this, "名称已经存在。");
                return;
            }

            fanli.Model.fanli_helpcata model = dal.GetModel(Id);
            if (model!=null)
            {
                model.cataname = varCataname;	//类别名称
                model.about = varAbout;	//说明
                model.sortid = varSortid;	//排序                
            }
            else
            {
                LIB.JsWrite.Alert(this, "无法读取对象！");
                return;
            }
            #endregion

            if (dal.Update(model) > 0)
            {
                LIB.MessageBox.ShowAndClose(this, "修改成功");
            }
            else
            {
                LIB.MessageBox.ShowAndClose(this, "修改失败");
            }
        }
    }
}